﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Security.Claims;
using SystemWFH.API.Application.Command.EquipmentCommand;
using SystemWFH.API.Application.Command.WorkshopCommand;
using SystemWFH.API.DTO;
using SystemWFH.Domain;

namespace SystemWFH.API.Controllers
{
    /// <summary>
    /// 车间管理--API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    [AllowAnonymous]
    public class WorkshopsController : ControllerBase
    {
        /// <summary>
        /// 中介者服务
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">服务</param>
        public WorkshopsController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 车间管理显示+查询+分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<PageDTO<Workshop>>> GetAsync([FromQuery] WorkshopGetAllCommand workshopGetAll)
        {
            ResultAPI<PageDTO<Workshop>> result = new ResultAPI<PageDTO<Workshop>>();
            try
            {
                result.Data=  await mediator.Send(workshopGetAll);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理添加
        /// </summary>
        /// <param name="workshop">实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ResultAPI<int>> Add(WorkshopAddCommand workshop)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
               result.Data= await mediator.Send(workshop);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理批量删除
        /// </summary>
        /// <param name="workshopDelete">编号ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ResultAPI<int>> Delete(WorkshopDeleteCommand workshopDelete)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data= await mediator.Send(workshopDelete);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理单删除
        /// </summary>
        /// <param name="workshopDelete">编号ID</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ResultAPI<int>> Deletes(WorkshopDeletesCommand workshopDelete)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data = await mediator.Send(workshopDelete);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理反填
        /// </summary>
        /// <param name="workshopFind"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<Workshop>> GetByIdAsync([FromQuery] WorkshopFindCommand workshopFind)
        {
            ResultAPI<Workshop> result = new ResultAPI<Workshop>();
            try
            {
                result.Data= await mediator.Send(workshopFind);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理修改
        /// </summary>
        /// <param name="workshopUpdate"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultAPI<int>> WorkAsync(WorkshopUpdateCommand workshopUpdate)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data= await mediator.Send(workshopUpdate);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 车间管理状态修改
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultAPI<int>> WorkshopStateUpdate([FromQuery] WorkshopStateCommand workshop)
        {
            ResultAPI<int> result = new ResultAPI<int>();
            try
            {
                result.Data = await mediator.Send(workshop);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
